package com.unitd.modules.configcenter.controller;

import com.github.pagehelper.PageInfo;
import com.unitd.frame.comm.utils.BeanUtils;
import com.unitd.frame.comm.utils.NumberUtils;
import com.unitd.frame.comm.utils.StringUtils;
import com.unitd.frame.springweb.controller.result.AjaxResult;
import com.unitd.frame.springweb.controller.result.PageResult;
import com.unitd.modules.comm.exception.ConfigServerException;
import com.unitd.modules.comm.model.SelectOption;
import com.unitd.modules.comm.web.BaseController;
import com.unitd.modules.configcenter.entity.Apps;
import com.unitd.modules.configcenter.entity.Users;
import com.unitd.modules.configcenter.service.IAppsService;
import com.unitd.modules.configcenter.service.IUsersService;
import com.unitd.modules.sso.util.SecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @desc 配置中心应用信息管理前端控制器
 * @filename AppController.java
 * @copyright www.unitd.com
 * @version 1.0
 * @author Hudan
 * @date 2018/1/31
 */
@RestController
@RequestMapping("/admin/app")
public class AppController extends BaseController {

	@Autowired
	private IAppsService appsService;
	@Autowired
	private IUsersService usersService;

	/**
	 * @desc 获取信息列表json数据,方便列表页通过ajax获取并展示
	 * @param param 配置中心应用信息
	 * @return json数据
	 */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public PageResult list(Apps param) {
		// 获取分页数据信息
		Map<String, Integer> pageMap = getPageMap();

		// 查询列表数据
		PageInfo<Apps> pageData = appsService.getPageData(pageMap.get("pageNo"), pageMap.get("pageSize"), param);

		return new PageResult(pageData.getTotal(), pageData.getList());
	}

	/**
	 * @desc 根据主键ID,获取对应的详情信息
	 * @param id 主键ID
	 * @return 自定义的通用json对象
	 */
	@RequestMapping(value = "{id}", method = RequestMethod.GET)
	public AjaxResult getData(@PathVariable("id") String id) {
		// 根据主键获取属性配置所属环境信息
		Apps entity = appsService.selectByPrimaryKey(id);
		return new AjaxResult(entity);
	}

	/**
	 * @desc 新增/修改数据
	 * @param param 前端需要进行新增/修改的数据
	 * @return 自定义的通用json对象
	 */
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	public AjaxResult edit(@RequestBody Apps param) {
		SecurityUtil.requireSuperAdmin();

		// 新增数据
		if(StringUtils.isNull(param.getId())) {
			param.setId(null);
			Apps data = appsService.findByName(param.getName());
			if (data != null) {
				throw new ConfigServerException(1002, "已存在名为[" + param.getName() + "]的应用信息!");
			}

			// 只有项目负责人不为空的时候,才设置关联对象信息
			if (!NumberUtils.isZero(param.getMasterUid())) {
				Users master = usersService.selectByPrimaryKey(param.getMasterUid());
				param.setMaster(master.getName());
			}
			appsService.insertSelective(param);
		} else {
			// 修改数据
			Apps data = appsService.selectByPrimaryKey(param.getId());
			if(BeanUtils.isNull(data)) {
				throw new ConfigServerException(1002, "当前应用不存在!");
			}

			if(!param.getName().equalsIgnoreCase(data.getName())) {
				Apps oldData = appsService.findByName(param.getName());
				if (oldData != null) {
					throw new ConfigServerException(1002, "已存在名为[" + param.getName() + "]的应用信息!");
				}
			}
			appsService.updateByPrimaryKeySelective(param);
		}

		return new AjaxResult(true, "数据保存成功!");
	}

	/**
	 * @desc 根据主键ID,删除响应的数据
	 * @param id 主键ID
	 * @return 自定义的通用json对象
	 */
	@RequestMapping(value = "delete/{id}", method = RequestMethod.GET)
	public AjaxResult delete(@PathVariable("id") String id) {
		SecurityUtil.requireSuperAdmin();

		int delete = appsService.deleteByPrimaryKey(id);
		if(delete > 0) {
			return new AjaxResult(true, "删除数据成功!");
		} else {

			return new AjaxResult(false, "删除数据失败!");
		}
	}

	/**
	 * @desc 获取有效的信息的下拉菜单列表信息
	 * @return 下拉菜单列表数据
	 */
	@RequestMapping(value = "options", method = RequestMethod.GET)
	public List<SelectOption> getSelectOptionData() {
		List<SelectOption> result = new ArrayList<>();
		List<Apps> list;
		if (SecurityUtil.isSuperAdmin()) {
			list = appsService.listAll();
		} else {
			list = appsService.findByMasterUid(SecurityUtil.getLoginUserInfo().getUid());
		}
		for (Apps entity : list) {
			result.add(new SelectOption(String.valueOf(entity.getId()), entity.getAlias()));
		}
		return result;
	}
}